package com.neuedu.web.service;

import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.DeptDao;
import com.neuedu.web.dao.UmsUserDao;
import com.neuedu.web.po.DeptPO;
import com.neuedu.web.po.UmsUser;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.DeptVO;
import com.neuedu.web.vo.UserVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述  Dept 业务工呢
 * @data 2024年11月21日14:23:14
 */
public class DeptService {

    private DeptDao deptDao = new DeptDao();


    public List list(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from dept";
        List<DeptPO> list = deptDao.selectAll(sql);

        return BeanConvertUtils.po2Vo(list, DeptVO.class);

    }


    public PageInfo<DeptVO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from dept";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<DeptPO> dataList = deptDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = deptDao.total(sql);

        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(BeanConvertUtils.po2Vo(dataList, DeptVO.class));

        return pageInfo;

    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     */
    public boolean save(HttpServletRequest request, HttpServletResponse response) {
        DeptPO data = ParamUtils.getParam(DeptPO.class, request);
        if(data.getDeptId() != null){
            return  deptDao.update(data) >0;

        }else{
            return  deptDao.insert(data) >0;

        }
    }

    public boolean delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        return deptDao.delete(Long.parseLong(id)) >0;
    }


    public DeptVO getById(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");

        return BeanConvertUtils.conver(DeptVO.class,deptDao.selectById(Long.parseLong(id))) ;
    }
}
